"use strict";

import axios, { AxiosRequestConfig, AxiosRequestHeaders } from "axios";


let config = {
  // baseURL: process.env.baseURL || process.env.apiUrl || ""
  timeout: 600 * 1000, // Timeout
  // withCredentials: true, // Check cross-site Access-Control
};

const _axios = axios.create(config);

_axios.interceptors.request.use(
  function(config) {
    // Do something before request is sent
    return config;
  },
  function(error) {
    // Do something with request error
    return Promise.reject(error);
  }
);

// Add a response interceptor
_axios.interceptors.response.use(
  function(response) {
    // Do something with response data
    if (response.status == 200) {
      return response
      // let code = response.data.returnCode;
      // if(code == 0){
      //   return response.data.returnData;
      // }
      // else {
      //     return Promise.reject(response.data)
      // }
    }
    else{
        return Promise.reject('error')
    }
  },
  function(error) {
    // Do something with response error
    return Promise.reject(error);
  }
);

export function getData(url: string, params: any, config: AxiosRequestConfig) {
  return _axios.get(url, {params, ...config})
}

export function postData(url: string, params: any) {
  return _axios.post(url, params)
}

